package com.bit.demo3;
//=============================JAVA15课时.2：36处=================================

import java.util.Arrays;
import java.util.Comparator;
//以下代码会报错，不能够直接比较
//class Student {
//    private String name;
//    private int score;
//
//    public Student(String name, int score) {
//        this.name = name;
//        this.score = score;
//    }
//
//    @Override
//    public String toString() {
//        return "[" + this.name + ":" + this.score + "]";
//    }
//}

//public class TestDemo5 {
//    public static void main(String[] args) {
//        Student[] students = new Student[]{
//                new Student("张三", 95),
//                new Student("李四", 96),
//                new Student("王五", 97),
//                new Student("赵六", 92),
//        };
//        Arrays.sort(students);
//        System.out.println(Arrays.toString(students));
//    }
//}


//====================Comparable接口：对类的侵入性非常强，一旦写好了就不敢轻易改动=======================
//class Student implements Comparable {
//    private String name;
//    private int score;
//
//    public Student(String name, int score) {
//        this.name = name;
//        this.score = score;
//    }
//
//    @Override
//    public String toString() {
//        return "[" + this.name + ":" + this.score + "]";
//    }
//
//    @Override
//    public int compareTo(Object o) {
//        Student s = (Student) o;
//        if (this.score > s.score) {//this  谁调用compareTo谁就是this
//            return -1;
//        } else if (this.score < s.score) {
//            return 1;
//        } else {
//            return 0;
//        }
//    }
//}
//
//public class TestDemo5 {
//    public static void main(String[] args) {
//        Student[] students = new Student[]{
//                new Student("张三", 95),
//                new Student("李四", 96),
//                new Student("王五", 97),
//                new Student("赵六", 92),
//        };
//        Arrays.sort(students);
//        System.out.println(Arrays.toString(students));
//    }
//
//}


//*****************Comparator接口*******************
//class Student {
//    private String name;
//    private int score;
//    public int age;
//
//    public Student(String name, int score, int age) {
//        this.name = name;
//        this.score = score;
//        this.age = age;
//    }
//
//    static class AgeComparator implements Comparator<Student> {
//        @Override
//        public int compare(Student o1, Student o2) {
//            return o1.age - o2.age;
//        }
//    }
//
//    @Override
//    public String toString() {
//        return "[" + this.name + ":" + this.score + " " + this.age + "]";
//    }
//
//
//}

//public class TestDemo5 {
//    public static void main(String[] args) {
//        Student[] student = new Student[4];
//        student[0] = new Student("张三", 95, 19);
//        student[1] = new Student("李四", 96, 15);
//        student[2] = new Student("王五", 97, 24);
//        student[3] = new Student("赵六", 92, 21);
//        Student.AgeComparator ageComparator = new Student.AgeComparator();
//        System.out.println(ageComparator.compare(student[0], student[1]));
//    }
//
//
//}


//*************clone接口******************可以创建对象*******
//class Person implements Cloneable{ //cloneable是一个标志接口，为空接口，表示可以克隆
//    public int age;
//    public void eat(){
//        System.out.println("吃！");
//    }
//
//    @Override
//    public String toString() {
//        return "Person{" +
//                "age=" + age +
//                '}';
//    }
//
//    //要调用Clone方法，就必须要重写Clone
//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }
//}


//public class TestDemo5 {
//    public static void main(String[] args) throws CloneNotSupportedException {
//        Person person = new Person();
//        person.age = 99;
//
//        Person person1 = (Person)person.clone();
//        System.out.println(person1);
//
//        System.out.println("====================");
//        person1.age = 199;
//
//        System.out.println(person);
//        System.out.println(person1);
//    }
//
//}




//====================================================================================
//***************理解深浅拷贝****************17课时30分钟处
//浅拷贝：浅拷贝仅仅复制所考虑的对象，而不复制它所引用的对象
//class Money {
//    double m = 12.5;
//}
//
//class Person implements Cloneable {
//    public int age;
//    public Money money = new Money();
//
//    @Override
//    public String toString() {
//        return "Person{" +
//                "age=" + age +
//                ", money=" + money +
//                '}';
//    }
//
//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }
//}
//
//public class TestDemo5 {
//    public static void main(String[] args) throws CloneNotSupportedException {//抛出异常
//        Person person = new Person();
//        Person person1 = (Person) person.clone();
//        System.out.println(person.money.m);
//        System.out.println(person1.money.m);
//        System.out.println("======================================");
//        person1.money.m = 99.99;
//        System.out.println(person.money.m);
//        System.out.println(person1.money.m);
//        System.out.println("可以看出，即使改变了person1.money.m的值，person. . 的值也会跟着改变，即person1.money的引用还是指向person.money的引用  此为浅拷贝");
//    }
//}


//深拷贝：把要复制的对象所引用的对象都复制了一遍
class Money implements Cloneable {
    double m = 12.5;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable {
    public int age;
    public Money money = new Money();

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", money=" + money +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person) super.clone();//将person克隆 命名为tmp
        tmp.money = (Money) this.money.clone();//person.money也要克隆，变成tmp.money 即克隆了person.money的引用m
        return tmp;
    }
}

public class TestDemo5 {
    public static void main(String[] args) throws CloneNotSupportedException {//抛出异常
        Person person = new Person();
        Person person1 = (Person) person.clone();
        System.out.println(person.money.m);
        System.out.println(person1.money.m);
        System.out.println("======================================");
        person1.money.m = 99.99;
        System.out.println(person.money.m);
        System.out.println(person1.money.m);
        System.out.println("可以看出，改变了person1.money.m的值，person. . 的值不会跟着改变，即person1.money指向了新的的引用  此为深拷贝");
    }
}
